//
//  SellTableViewController.m
//  CarTown
//
//  Created by xionghao on 15/8/5.
//  Copyright (c) 2015年 熊浩. All rights reserved.
//

#import "SellTableViewController.h"
#import "CarTableCellTableViewCell.h"
#import "ShopUserItem.h"
#import "CarDetailmodel.h"
#import "CarlistCellMode.h"
#import <MJRefresh.h>
#import "CarDetailViewController.h"
#import "IndexCarModel.h"
#import "ShareView.h"
#import "UMSocial.h"
@interface SellTableViewController ()<ShareclickDelegate>
@property (nonatomic,strong) NSMutableArray     *data;
@property (nonatomic,assign) BOOL isUser;
@property (nonatomic,copy) NSString *cond;
@end

@implementation SellTableViewController

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
-(void)shareclickWithIndex:(NSInteger)index{
    [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
    switch (index) {
        case 0:
        {
            
            
            [UMSocialData defaultData].extConfig.wechatSessionData.title =self.sharemode.sharetitle;
             [UMSocialData defaultData].extConfig.wechatSessionData.url=self.sharemode.url;
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatSession] content:self.sharemode.sharecontent image:self.sharemode.logo location:nil urlResource:self.sharemode.resource presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
        case 1:
        {
            
            
            [UMSocialData defaultData].extConfig.wechatTimelineData.title =self.sharemode.sharetitle;
             [UMSocialData defaultData].extConfig.wechatTimelineData.url=self.sharemode.url;
            [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToWechatTimeline] content:self.sharemode.sharecontent image:self.sharemode.logo location:nil urlResource:self.sharemode.resource presentedController:self completion:^(UMSocialResponseEntity *response){
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"分享成功！");
                }
            }];
        }
            break;
            
        case 2:
        {
            
            
            
        }
            break;
            
        default:
            break;
    }

}
-(void)share{
    NSArray *titlearray=@[@"微信",@"朋友圈",@"短信"];
    UIImage *weixin=[UIImage imageNamed:@"weixin"];
    UIImage *pengyou=[UIImage imageNamed:@"liulan"];
    UIImage *sms=[UIImage imageNamed:@"duanxin"];
    NSArray *icon=@[weixin,pengyou,sms];
    ShareView *share=[[ShareView alloc]initWithTitle:titlearray andIcons:icon delegate:self];
    [share show];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"可售车源";
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"top-r-two"] style:UIBarButtonItemStyleDone target:self action:@selector(share)];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    [self setuptableview];
   
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(NSMutableArray *)data{
    if ( _data==nil) {
        _data=[NSMutableArray array];
    }
    return  _data;
}

-(void)setuptableview{
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    MJRefreshBackNormalFooter *footer=[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadmoredata)];
    self.tableView.header=header;
    self.tableView.footer=footer;
    [self.tableView.header beginRefreshing];
}
-(NSString *)getcond{
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    
    if (self.userid) {
        dict[@"user_id"]=self.userid;
        self.isUser=YES;
    }else {
            dict[@"parent_id"]=self.parentid;
            dict[@"com_id"]=self.comid;
       
    }
    return [NSString DataTOjsonString:dict];
}
-(void)loadmoredata{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    
        params[@"action"]=@"where";
        params[@"cond"]=[self getcond];
        

        params[@"action"]=@"where";
    
        params[@"num"]=Kpagesize;
    NSMutableArray *IDSarray=[NSMutableArray array];
    for (CarlistCellMode *mode in self.data) {
        [IDSarray addObject:mode.cardmode.ID];
    }
    
    params[@"page_id"]=[NSString DataTOjsonString:IDSarray];
    [PostUitls Post:sscarurl parameters:params success:^(id response) {
        NSDictionary *dict=response;
        IndexCarModel *carmode=[IndexCarModel indexcarWithDcit:dict];
        if ([carmode.state integerValue]==400) {
            NSMutableArray *marray=[NSMutableArray array];
            for (CarDetailmodel *cardetail in carmode.data) {
                CarlistCellMode *carlistcell=[[CarlistCellMode alloc]init];
                carlistcell.cardmode=cardetail;
                
                [marray addObject:carlistcell];
            }
            [self.data addObjectsFromArray:marray];
            
            [self.tableView.footer endRefreshing];
            [self.tableView reloadData];
            
        }else{
            [self.tableView.footer noticeNoMoreData];
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=carmode.msg;
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
            NSLog(@"%@",carmode.msg);
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [self.tableView.footer endRefreshing];
    }];
}

-(void)refresh{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    
        params[@"action"]=@"where";
        params[@"cond"]=[self getcond];
        
        params[@"num"]=Kpagesize;
    
    [PostUitls Post:sscarurl parameters:params success:^(id response) {
        
        NSDictionary *dict=response;
        IndexCarModel *carmode=[IndexCarModel indexcarWithDcit:dict];
        if ([carmode.state integerValue]==400) {
            [self.data removeAllObjects];
            NSMutableArray *marray=[NSMutableArray array];
            for (CarDetailmodel *cardetail in carmode.data) {
                CarlistCellMode *carlistcell=[[CarlistCellMode alloc]init];
                carlistcell.cardmode=cardetail;
                carlistcell.title=cardetail.shop_name;
                [marray addObject:carlistcell];
            }
            if (marray.count>=[Kpagesize integerValue]) {
                [self.tableView.footer resetNoMoreData];
            }else{
                [self.tableView.footer noticeNoMoreData];
            }
            [self.data addObjectsFromArray:marray];
            [self.tableView.header endRefreshing];
            [self.tableView reloadData];
            
        }else{
            
            [self.tableView.header endRefreshing];
            MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.labelText=carmode.msg;
            hud.mode = MBProgressHUDModeText;
            hud.margin = 10.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:1];
            
            NSLog(@"%@",carmode.msg);
        }
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
        [self.tableView.header endRefreshing];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.data.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarlistCellMode *mode=self.data[indexPath.row];
    
    CarTableCellTableViewCell *cell=[CarTableCellTableViewCell cellWithTableView:tableView];
    cell.carcellmode=mode;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CarlistCellMode *mode=self.data[indexPath.row];
    return mode.rowheight;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CarlistCellMode *mode=self.data[indexPath.row];
    CarDetailViewController *cadvc=[CarDetailViewController viewcontroller];
    cadvc.ID=mode.cardmode.ID;
    [self.navigationController pushViewController:cadvc animated:YES];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
